package com.javatom.authority;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
//import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

/**
 * 服务配置<br>
 *
 * @className: AuthorityServerConfig
 * @package: com.javatom.notes.authority
 * @author: 曾维录
 * @date: 2019/7/4 17:16
 */
@Configuration
@EnableAuthorizationServer
public class AuthorityServerConfig extends AuthorizationServerConfigurerAdapter {
    /**
     * 这个必须写在最上面
     */
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private PasswordEncoder passwordEncoder;
    //------token配置------------
//    @Autowired
//    private TokenStore tokenStore;
//    @Autowired
//    private ClientDetailsService clientDetailsService;
    //----------------------------

//    @Autowired
//    private AuthorizationCodeServices authorizationCodeServices;
    //----------------------------

    @Autowired
    private UserDetailsService userDetailsService;

//    @Autowired
//    private AuthenticationManager authenticationManager;
////
////    @Autowired
////    private DataSource dataSource;
//
//    @Autowired
//    private UserDetailsService userDetailsService;
//    @Autowired
//    private PasswordEncoder passwordEncoder;
    @Bean
    public TokenStore tokenStore() {

        return new InMemoryTokenStore();
    }
    /**
     *
     * configure(ClientDetailsServiceConfigurer clients)
     * @return
     */
//    public AuthorizationServerTokenServices authorizationServerTokenServices() {
//        DefaultTokenServices service = new DefaultTokenServices();
//        service.setClientDetailsService(clientDetailsService);//客户端详情服务
//        service.setSupportRefreshToken(true);//支持刷新令牌
//        service.setTokenStore(tokenStore);//令牌存储策略
//        service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
//        service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
//        return  service;
//    }
    //----------------------------
    /**
     * 配置访问端点
     * @param endpoints endpoints
     * @throws Exception e
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)// add get method
                .userDetailsService(userDetailsService)
                .tokenStore(tokenStore())

                .authenticationManager(authenticationManager);


    }
    /**
     * 配置资源服务器
     * @param clients clients
     * @throws Exception e
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()//使用内存存储
                .withClient("app") //clientid 用于标识客户的id
                .secret(passwordEncoder.encode("gateway")) //sercret 需要值得信任的客户端，客户端安全码
                .resourceIds("app") //访问的资源列表
                .authorizedGrantTypes("authorization_code","password","client_credentials",
                        "implicit","refresh_token") //authorizedGrantTypes 此客户端可以使用的授权类型 /authorities 此客户端客户使用的权限
                .scopes("all") // scope 用户限制客户端的访问范围
                .autoApprove(false) //false 跳转到授权页面
                .redirectUris("http://www.baidu.com");
    }
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
                .tokenKeyAccess("permitAll()")                    //oauth/token_key是公开
                .checkTokenAccess("permitAll()")                  //oauth/check_token公开
                .allowFormAuthenticationForClients();
    }




}